<template>
  <div class="app-container">
    <el-row :gutter="10" class="search-top">
      <el-col :span="1.5">
        <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="100px">
          <el-form-item label="软件包名称" prop="packageName">
            <el-input
              v-model="queryParams.packageName"
              placeholder="请输入软件包名称"
              clearable
              @keyup.enter.native="handleQuery"
            >
            <el-button slot="append" icon="el-icon-search" @click="handleQuery" ></el-button>
            </el-input>
          </el-form-item>
        </el-form>
      </el-col>
      <el-col :span="1.5" style="float: right;">
        <el-button
          type="primary"
          plain
          icon="el-icon-upload"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['attachments:attachments:add']"
        >上传软件包</el-button>
      </el-col>
    </el-row>
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['attachments:attachments:remove']"
        >批量删除</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="attachmentsList"
              highlight-selection-row
              @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="ID" align="center" prop="id" width="80"/>
      <el-table-column label="软件包名" align="center" prop="packageName" width="300" show-overflow-tooltip/>
      <el-table-column label="类型" align="center" prop="packageType"/>
      <el-table-column label="版本" align="center" prop="version" />
      <el-table-column label="系统" align="center" prop="osVersion"/>
      <el-table-column label="硬件架构" align="center" prop="cpuArch"/>
      <el-table-column label="大小（MB）" align="center" prop="packageSize" >
        <template slot-scope="scope">
          {{parseFloat(scope.row.packageSize/1024/1024).toFixed(2)}}
        </template>
      </el-table-column>
      <el-table-column label="上传时间" align="center" prop="createTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-download"
            @click="handleDownload(scope.row)"
            v-hasPermi="['attachments:attachments:download']"
          >下载</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['attachments:attachments:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
    <upload-file v-if="open" @closeDrawer="getList"/>
  </div>
</template>

<script>
import { listAttachments, delAttachments, addAttachments } from "@/api/attachments/attachments";
import UploadFile from "@/views/attachments/upload";
export default {
  name: "Attachments",
  components:{
    UploadFile
  },
  dicts: ['kw_system_version','kw_system_arch'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 软件包管理表格数据
      attachmentsList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        packageType: null,
        packageName: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        osVersion: [
          { required: true, message: "请输选择操作系统", trigger: "blur" }
        ],
        cpuArch: [
          { required: true, message: "请输选择硬件架构", trigger: "blur" }
        ],
        version: [
          { required: true, message: "请输入版本号", trigger: "blur" }
        ],
        packagePath: [
          { required: true, message: "请上传软件包", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询软件包管理列表 */
    getList() {
      this.loading = true;
      this.open =false;
      listAttachments(this.queryParams).then(response => {
        this.attachmentsList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        packageType: null,
        packageName: null,
        packagePath: null,
        packageSize: null,
        version: null,
        osVersion: null,
        cpuArch: null,
        createTime: null,
        createUser: null,
        updateTime: null,
        updateUser: null,
        comment: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.open = true;
    },
    /** 修改按钮操作 */
    handleDownload(row) {
      var name = row.packageName;
      var url = row.packagePath;
      const a = document.createElement('a')
      a.setAttribute('download', name)
      a.setAttribute('target', '_blank')
      a.setAttribute('href', url)
      a.click()
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateAttachments(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addAttachments(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除软件包ID为"' + ids + '"的数据项？').then(function() {
        return delAttachments(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
  }
};
</script>
